﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BasicModuleOrm.Models;
using Microsoft.EntityFrameworkCore;
using BasicModuleOrm.Db_Context;
using BasicModuleOrm.Model;
using System.Collections.Immutable;
using Microsoft.EntityFrameworkCore.Internal;
using BasicModuleOrm.ProductRespository;
namespace BasicModuleOrm.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProudctRespositoryController : ControllerBase
    {
        private IProductRespository _productRespository;

        public ProudctRespositoryController(IProductRespository productRespository)
        {
            _productRespository = productRespository;
        }

        /// <summary>
        /// 查询所有商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<Product> GetProducts()
        {
            return _productRespository.GetAllProduct();
        }


        /// <summary>
        /// 查询商品根据商品Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<Product> GetProduct(int id)
        {
            // 1、查询商品
            var product = _productRespository.GetProduct(id);

            return product;
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<Product> PostProduct(Product product)
        {
            // 1、添加商品
            _productRespository.AddProduct(product);

            return CreatedAtAction(nameof(PostProduct), new { id = product.Id }, product);
        }


        /// <summary>
        /// 更新商品通过Id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public IActionResult PutProduct(int id, Product product)
        {
            _productRespository.UpdateProduct(id, product);

            return Ok("更新成功");
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteProduct(int id)
        {
            _productRespository.DeleteProduct(id);

            return Ok("删除成功");
        }
    }
}
